import cPickle
import sys
import os
from collections import Counter
from cdf import *
from collections import defaultdict
import numpy as np 
from numpy import arange,array,ones,linalg

xindex={}
map_db="az_map.db"
c1_db="clus_1.db"
c2_db="clus_2.db"
cno_db="clus_no.db"
for x in open("xindex.db"):
	xindex[x.strip("\n")]=1

def init_bl():
	return {}

def init_db(dbn,blacklist):
	db=cPickle.load(open(dbn))
	for k in db.keys():
		if k in blacklist:
			db.pop(k)
	return db

def init_matrix(db):
	m={}
	for k in db:
		c=db[k]
		ip,tm=k
		if tm not in xindex:
			continue
		if c not in m:
			m[c]={}
		if tm in m[c]:
			m[c][tm].append(ip)
		else:
			m[c][tm]=[ip]
	return m

cls_blacklist=init_bl()
#db=init_db(c2_db,cls_blacklist)
matrix=init_matrix(init_db(c2_db,cls_blacklist))

def if_same_web(m):
	for k in m:
		tk=k.keys()[0]
		v=k[tk]

def server_update():
	db=init_db(cno_db,cls_blacklist)
	mt={}
	for k in db:
		kt=k[:4]
		server=k[4]
		chash=k[5]
		c=db[k]
		tmp={}
		if kt in mt:
			mt[kt].append((server,chash,c))
		else:
			mt[kt]=[]
			mt[kt].append((server,chash,c))

	for k in mt.keys():
		if len(mt[k])==1:
			mt.pop(k)
	
	for k in mt.keys():
		s=[v[0] for v in mt[k]]
		s=list(set(s))
		if len(s)==1:
			mt.pop(k)
	
	for k in mt.keys():
		s=[v[1] for v in mt[k]]
		s=list(set(s))
		if len(s)!=1:
			mt.pop(k)
	res={}
	for k in mt:
		for index in xrange(len(mt[k])):
			for nindex in range(index+1,len(mt[k])):
				if index==nindex:
					continue
				cserver=mt[k][index][0]
				nserver=mt[k][nindex][0]
				if cserver.split("/")[0]!=nserver.split("/")[0]:
					continue
				try:
					if cserver.split("/")[1]<nserver.split("/")[1]:
						cno=mt[k][index][2]
						nno=mt[k][nindex][2]
						mmax=sorted(matrix[cno])[-1]
						mmin=sorted(matrix[nno])[0]
						if mmax<mmin:
							if k in res:
								res[k].append((cno,nno))
							else:
								res[k]=[(cno,nno)]
					else:
						cno=mt[k][index][2]
						nno=mt[k][nindex][2]
						mmax=sorted(matrix[nno])[-1]
						mmin=sorted(matrix[cno])[0]
						if mmax<mmin:
							if k in res:
								res[k].append((nno,cno))
							else:
								res[k]=[(nno,cno)]
				except:
					continue

	print len(res)

def tmpl_update():
	db=init_db(cno_db,cls_blacklist)
	mt={}
	for k in db:
		kt=(k[0],k[1],k[3],k[4])
		tmpl=k[2]
		chash=k[5]
		c=db[k]
		tmp={}
		if kt in mt:
			mt[kt].append((tmpl,chash,c))
		else:
			mt[kt]=[]
			mt[kt].append((tmpl,chash,c))

	for k in mt.keys():
		if len(mt[k])==1:
			mt.pop(k)
	
	for k in mt.keys():
		s=[v[0] for v in mt[k]]
		s=list(set(s))
		if len(s)==1:
			mt.pop(k)
	
	
	res={}
	for k in mt:
		for index in xrange(len(mt[k])):
			for nindex in range(index+1,len(mt[k])):
				if index==nindex:
					continue
				cserver=mt[k][index][0]
				nserver=mt[k][nindex][0]
				if cserver.split(" ")[0]!=nserver.split(" ")[0]:
					continue
				try:
					if cserver.split(" ")[1]<nserver.split(" ")[1]:
						cno=mt[k][index][2]
						nno=mt[k][nindex][2]
						mmax=sorted(matrix[cno])[-1]
						mmin=sorted(matrix[nno])[0]
						if mmax<mmin:
							if k in res:
								res[k].append((cno,nno))
							else:
								res[k]=[(cno,nno)]
					else:
						cno=mt[k][index][2]
						nno=mt[k][nindex][2]
						mmax=sorted(matrix[nno])[-1]
						mmin=sorted(matrix[cno])[0]
						if mmax<mmin:
							if k in res:
								res[k].append((nno,cno))
							else:
								res[k]=[(nno,cno)]
				except:
					continue

	print len(res)





			








if __name__ == '__main__':
	tmpl_update()
	#dt=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	#print len(set(dt))
	#if_incr(dt)